home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / tvalt.exe / GFX.CPP < prev    next >
C/C++ Source or Header  |  1992-10-05  |  4KB  |  161 lines

  1. #define Uses_TMouse
  2. #define Uses_TEvent
  3. #include <tv.h>
  4.  
  5. #include <graphics.h>
  6.  
  7. #define HIBYTE(x) ( ((x) >> 8) & 0xFF )
  8. #define LOBYTE(x) ( (x) & 0xFF )
  9. #define BGATTR(x) ( ((HIBYTE(x)) >> 4) & 0x0F )
  10. #define FGATTR(x) ( (HIBYTE(x)) & 0x0F )
  11.  
  12. static unsigned char strbuf[100];
  13. extern unsigned char shadowAttr;
  14.  
  15. // function: paintBkgd
  16. // declaration:
  17. //    short paintBkgd( short ix, short iy, short max, ushort *buf );
  18. // where:
  19. //        ix  = start horizontal position (in pixels).
  20. //        iy  = start vertical position (in pixels).
  21. //        max = the length of buf (in words).
  22. //        buf = the char/attr buffer.
  23. // paintBkgd() searches buf for the maximum number of entries that have the
  24. //    same background color (up to max), then paints that background 'bar'
  25. //    to the screen at (ix,iy). It is assumed that the width and height of
  26. //    each character is 8x8 pixels. Returns the number of entries whose
  27. //    background was painted.
  28.  
  29. short paintBkgd( short ix, short iy, short max, ushort *buf )
  30.     {
  31.     uchar attr = BGATTR(*buf);
  32.     short count = 0;
  33.     while( count < max && BGATTR(buf[count]) == attr )
  34.         count++;
  35.     setfillstyle( SOLID_FILL, attr );
  36.     bar( ix, iy, ix+(count << 3)-1, iy+7 );
  37.     return count;
  38.     }
  39.  
  40. // function: paintFrgd
  41. // declaration:
  42. //    short paintFrgd( short ix, short iy, short max, ushort *buf );
  43. // where:
  44. //        ix  = start horizontal position (in pixels).
  45. //        iy  = start vertical position (in pixels).
  46. //        max = the length of buf (in words).
  47. //        buf = the char/attr buffer.
  48. // paintFrgd() skips leading spaces in buf's char data, then searches buf
  49. //    for the maximum number of entries that have the same foreground color
  50. //    (up to max), then paints this string to the screen at (ix,iy). Returns
  51. //    the number of characters (including the leading spacing) that was
  52. //    painted.
  53.  
  54. short paintFrgd( short ix, short iy, short max, ushort *buf )
  55.     {
  56.     uchar attr;
  57.     short fat = 0, count = 0;
  58.     char *ptr = strbuf;
  59.  
  60.     while( max > 0 && LOBYTE(*buf) == ' ' )
  61.         {
  62.         max--;
  63.         buf++;
  64.         ix += 8;
  65.         fat++;
  66.         }
  67.  
  68.     attr = FGATTR(*buf);
  69.     while( count < max && FGATTR(buf[count]) == attr )
  70.         {
  71.         *ptr = LOBYTE(buf[count]);
  72.         if( *ptr == ' ' )    break;
  73.         ptr++;
  74.         count++;
  75.         }
  76.     *ptr = 0;
  77.     setcolor(attr);
  78.     outtextxy( ix, iy, strbuf );
  79.     return count+fat;
  80.     }
  81.  
  82. // function: altWriteMethod
  83. // declaration:
  84. //    void altWriteMethod( short x, short y, short wid, ushort *buf,
  85. //                                short sflag );
  86. // where:
  87. //        x     = start horizontal position (in chars) : 0-based.
  88. //        y     = start vertical position (in chars) : 0-based.
  89. //        wid   = the length of buf (in words).
  90. //        buf   = the char/attr buffer.
  91. //        sflag = 0 if the attr entries of buf should be used; <> 0 if
  92. //                shadowAttr (extern) should be the attribute.
  93. // When alternative write method is enable, this function is used to write
  94. //    the TV char/attr data to the screen.
  95.  
  96. void altWriteMethod( short x, short y, short wid, ushort *buf, short sflag )
  97.     {
  98.     char *ptr = strbuf;
  99.     unsigned short far *pbuf;
  100.     short iw, count, ix, iy;
  101.  
  102.     // Its a little involved to ensure that the mouse cursor needs to be
  103.     // hidden - and the slowness of this graphics writing doesn't ensure
  104.     // that the mouse won't move into the paint region while we're writing
  105.     // so I just hide it every time.
  106.     TMouse::hide();
  107.  
  108.     // Paint the background colors.
  109.  
  110.     if( !sflag )        // use the attribute data in buf.
  111.         {
  112.         pbuf = buf;
  113.         iw = wid;
  114.         ix = x << 3;
  115.         iy = y << 3;
  116.         while( iw > 0 )
  117.             {
  118.             count = paintBkgd( ix, iy, iw, pbuf );
  119.             ix += (count << 3);
  120.             pbuf += count;
  121.             iw -= count;
  122.             }
  123.         }
  124.     else                // use shadowAttr attribute.
  125.         {
  126.         setfillstyle( SOLID_FILL, (shadowAttr >> 4) );
  127.         bar( x << 3, y << 3, ((x+wid-1) << 3)-1, ((y+1) << 3)-1 );
  128.         }
  129.  
  130.     // Paint the foreground characters.
  131.  
  132.     settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  133.     settextjustify( LEFT_TEXT, TOP_TEXT );
  134.  
  135.     if( !sflag )        // use the attribute data in buf.
  136.         {
  137.         pbuf = buf;
  138.         iw = wid;
  139.         ix = x << 3;
  140.         iy = y << 3;
  141.         while( iw > 0 )
  142.             {
  143.             count = paintFrgd( ix, iy, iw, pbuf );
  144.             ix += (count << 3);
  145.             pbuf += count;
  146.             iw -= count;
  147.             }
  148.         }
  149.     else                // use shadowAttr attribute.
  150.         {
  151.         for( count = 0; count < wid; count++ )
  152.             *ptr++ = LOBYTE(buf[count]);
  153.         *ptr = 0;
  154.         setcolor( shadowAttr & 0x0F );
  155.         outtextxy( x << 3, (y << 3), strbuf );
  156.         }
  157.  
  158.     // Restore the mouse.
  159.     TMouse::show();
  160.     }
  161.